package com.cskaoyan.weixin.service;

import com.cskaoyan.bean.User1;
import com.cskaoyan.mapper.CommentMapper;
import com.cskaoyan.mapper.OrderMapper;
import com.cskaoyan.mapper.UserMapper;
import com.cskaoyan.weixin.bean.user.UserOrderVO;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserWxServiceImpl implements UserWxService{

    @Autowired
    OrderMapper orderMapper;

    @Autowired
    CommentMapper commentMapper;

    @Autowired
    UserMapper userMapper;

    @Override
    public UserOrderVO queryOrderOfUser() {
        Integer userId = getUserIdByUsername();
        Integer unpaid = orderMapper.queryOrderOfUser(userId, (short) 101);
        Integer unship = orderMapper.queryOrderOfUser(userId, (short) 201);
        Integer unrecv = orderMapper.queryOrderOfUser(userId, (short) 301);

        Integer uncomment = orderMapper.queryNeedCommentOrder(userId);

        UserOrderVO userOrderVO = new UserOrderVO(0,0,0,0);
        if (unpaid != null) {
            userOrderVO.setUnpaid(unpaid);
        }
        if (unship != null) {
            userOrderVO.setUnship(unship);
        }
        if (unrecv != null) {
            userOrderVO.setUnrecv(unrecv);
        }
        if (uncomment != null) {
            userOrderVO.setUncomment(uncomment);
        }
        return userOrderVO;
    }


    private Integer getUserIdByUsername(){
        Subject subject = SecurityUtils.getSubject();
        PrincipalCollection principals = subject.getPrincipals();
        User1 primaryPrincipal = (User1) principals.getPrimaryPrincipal();
        Integer userId = userMapper.selectIdByUsername(primaryPrincipal.getUsername());
        return userId;
    }
}
